package top.hepingan.java.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import top.hepingan.java.common.Result;
import top.hepingan.java.dto.CaseImgDto;
import top.hepingan.java.entity.Cases;
import top.hepingan.java.entity.CaseImage;
import top.hepingan.java.entity.Orders;
import top.hepingan.java.entity.User;
import top.hepingan.java.mapper.CaseImageMapper;
import top.hepingan.java.mapper.CaseMapper;
import top.hepingan.java.mapper.OrdersMapper;
import top.hepingan.java.mapper.UserMapper;
import top.hepingan.java.utils.AliUploadUtils;
import top.hepingan.java.utils.BeanUtils;
import top.hepingan.java.vo.CaseVo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequestMapping("/admin")
@RestController
@Slf4j
@RequiredArgsConstructor
public class AdminController {

    private final CaseMapper caseMapper;
    private final CaseImageMapper caseImageMapper;
    private final UserMapper userMapper;
    private final OrdersMapper ordersMapper;
    private final AliUploadUtils aliUploadUtils;

    @PostMapping("/case/insert")
    public Result insertCase(@RequestBody CaseVo caseVo){
        Cases aCase = BeanUtils.copyClassProperTies(caseVo, Cases.class);
        caseMapper.insert(aCase);
        for (String image : caseVo.getImageList()){
            caseImageMapper.insert(new CaseImage().setCaseId(aCase.getId()).setImgUrl(image));
        }
        return Result.ok();
    }

    @GetMapping("/dashboard")
    public Result dashboard(){
        return Result.ok();
    }

    @GetMapping("/user/list")
    public Result userList(@RequestParam Integer pageNum){
        Page<User> page = new Page<>(pageNum, 20);
        return Result.data(userMapper.selectPage(page,new QueryWrapper<User>().lambda()
                .orderByDesc(User::getCreatedTime)));
    }

    @GetMapping("/order/list")
    public Result orderList(@RequestParam Integer pageNum,@RequestParam String status){
        Page<Orders> page = new Page<>(pageNum, 20);
        if (status != null && !status.isEmpty()){
            return Result.data(ordersMapper.selectPage(page,new QueryWrapper<Orders>().lambda()
                    .eq(Orders::getStatus,status)
                    .orderByDesc(Orders::getCreatedTime)).getRecords());
        }else {
            return Result.data(ordersMapper.selectPage(page,new QueryWrapper<Orders>().lambda()
                    .orderByDesc(Orders::getCreatedTime)).getRecords());
        }

    }

    @GetMapping("/case/list")
    public Result caseList(@RequestParam Integer pageNum,@RequestParam String status){
        List<Cases> records;
        if (Objects.equals(status, "all")){
            records= caseMapper.selectPage(new Page<>(pageNum, 20), new QueryWrapper<Cases>().lambda()
                    .orderByDesc(Cases::getCreatedTime)
                    .eq(Cases::getStatus,status)).getRecords();
        }else {
            records = caseMapper.selectPage(new Page<>(pageNum, 20), new QueryWrapper<Cases>().lambda()
                    .orderByDesc(Cases::getCreatedTime)).getRecords();
        }
        List<CaseVo> caseVos = new ArrayList<>();
        for (Cases cases : records){
            List<CaseImage> caseImages = caseImageMapper.selectList(new QueryWrapper<CaseImage>().lambda()
                    .eq(CaseImage::getCaseId, cases.getId()));
            List<String> imageList = new ArrayList<>();
            caseImages.forEach(caseImage -> imageList.add(caseImage.getImgUrl()));
            CaseVo caseVo = BeanUtils.copyClassProperTiesWriteDate(cases, CaseVo.class).setImageList(imageList);
            caseVos.add(caseVo);
        }
        return Result.data(caseVos);
    }

    @PostMapping("/case/add")
    public Result addCase(@RequestBody CaseVo caseVo){
        Cases cases = BeanUtils.copyClassProperTiesWriteDate(caseVo, Cases.class);
        caseMapper.insert(cases.setStatus("online").setCreatedTime(LocalDateTime.now()));
        caseVo.getImageList().forEach(image -> caseImageMapper.insert(new CaseImage().setCaseId(cases.getId()).setImgUrl(image)));
        return Result.ok();
    }

    @PostMapping("/case/delete")
    public Result deleteCase(@RequestBody Long id){
        caseMapper.deleteById(id);
        List<CaseImage> caseImages = caseImageMapper.selectList(new QueryWrapper<CaseImage>().lambda().eq(CaseImage::getCaseId, id));
        caseImages.forEach(caseImage -> aliUploadUtils.deleteFileByUrl(caseImage.getImgUrl()));
        caseImageMapper.delete(new QueryWrapper<CaseImage>().lambda().eq(CaseImage::getCaseId, id));
        return Result.ok();
    }

    /**
     * 更新时添加图片
     * @param caseImgDto
     * @return
     */
    @PostMapping("/case/addImg")
    public Result addCaseImg(@RequestBody CaseImgDto caseImgDto){
        caseImageMapper.insert(new CaseImage().setCaseId(caseImgDto.getId()).setImgUrl(caseImgDto.getImgUrl()));
        return Result.ok();
    }

    @PostMapping("/case/deleteImg")
    public Result deleteCaseImg(@RequestBody CaseImgDto caseImgDto){
        CaseImage caseImage = caseImageMapper.selectOne(new QueryWrapper<CaseImage>().lambda()
                .eq(CaseImage::getImgUrl, caseImgDto.getImgUrl())
                .eq(CaseImage::getCaseId, caseImgDto.getId()));
        aliUploadUtils.deleteFileByUrl(caseImage.getImgUrl());
        caseImageMapper.delete(new QueryWrapper<CaseImage>().lambda()
                .eq(CaseImage::getImgUrl, caseImgDto.getImgUrl())
                .eq(CaseImage::getCaseId, caseImgDto.getId()));
        return Result.ok();
    }

    @PostMapping("/case/update")
    public Result updateCase(@RequestBody Cases cases){
        caseMapper.updateById(cases);
        return Result.ok();
    }
}
